package com.waffles.mbta.tracker;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class BusStopOverlay extends ItemizedOverlay {
	
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private ArrayList<ArrayList<GeoPoint>> mPaths = new ArrayList<ArrayList<GeoPoint>>();
	
	BusStopOverlay(Drawable drawable){
		super(boundCenterBottom(drawable));
	}
	
	public void addPath(ArrayList<GeoPoint> points) {
		mPaths.add(points);	
	}
	
	public void addOverlay(OverlayItem toAdd) {
		mOverlays.add(toAdd);
		populate();
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		Projection proj = mapView.getProjection();
		Path path = new Path();
		Point point = new Point();
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.STROKE);
		
		for(ArrayList<GeoPoint> pointList : mPaths) {
			path.reset();	
				
			if(pointList.size() > 0) {
				proj.toPixels(pointList.get(0), point);
				path.moveTo(point.x, point.y);
			}
			
			for(GeoPoint geoPoint : pointList) {
				proj.toPixels(geoPoint, point);
				path.lineTo(point.x, point.y);
			}
			canvas.drawPath(path, paint);
		}
		
		super.draw(canvas, mapView, shadow);
	}

}
